package test.my.com.myapplicationtest.zhe_xian_tu_view.DynamicPathView.controller.utils;

import android.content.Context;
import android.graphics.Paint;
import android.util.Log;

/**
 * Created by MyPC on 2015/8/8.
 */
public class TextAttributeUtil {

    public static float getTextWidth(String str, float textSize){
        Paint paint = new Paint();
        paint.setTextSize(textSize);

        float width=paint.measureText(str);

        return width;
    }

    public static float getTextHeight(float textSize){
        float height =0;
        height = Math.abs(getTextFontMetrics(textSize).ascent)+ Math.abs(getTextFontMetrics(textSize).descent);
        return height;
    }

    public static Paint.FontMetrics getTextFontMetrics(float size) {
        Paint paint = new Paint();
        paint.setTextSize(size);
        return paint.getFontMetrics();
    }

    public static float getTWidth(Context context, String str, float textSize){
        Paint paint = new Paint();
        paint.setTextSize(textSize);
//        paint.setTypeface(FontUtil.getInstance(context).getTypeface());
        float width=paint.measureText(str);
        return width;
    }

    public static float getTHeight(Context context, float textSize){
          float height =0;
         height = Math.abs(getTextFontMetrics(context,textSize).ascent)+ Math.abs(getTextFontMetrics(context,textSize).descent);
        return height;
    }

    public static Paint.FontMetrics getTextFontMetrics(Context context, float size) {
        Paint paint = new Paint();
        paint.setTextSize(size);
//        paint.setTypeface(FontUtil.getInstance(context).getTypeface());
        return paint.getFontMetrics();
    }

    /**
     * 计算TextView放入文字后的高度（前提设置 android:includeFontPadding="false" 字体不是系统字体）
     * @param context
     * @param textSize 文字大小 px
     * @param str 文字内容
     * @param viewWidth TextView 的固定宽度
     * @param lineSpacingExtra  行间距
     * @return
     */
    public static int getTextHeightInView(Context context, int textSize, String str, int viewWidth, int lineSpacingExtra){
        if(getTWidth(context,str,textSize)<viewWidth){
            return textSize + lineSpacingExtra;
        }else{
            float width = TextAttributeUtil.getTWidth(context, str, textSize);
//            int num = 0;
//            if((width%viewWidth)!=0){
//                num = ((int)(width/viewWidth))+1;
//            }else {
//                num=(int)(width/viewWidth);
//            }
            int num = (int) Math.ceil(width / viewWidth);
            Log.e("ah","str width == "+width);
            Log.e("ah","viewWidth == "+viewWidth);
            Log.e("ah","width / viewWidth == "+(width / viewWidth));
            Log.e("ah","num == "+num);
            return num*textSize+num*lineSpacingExtra;
        }
    }

}
